'''
Created on 29/03/2011

@author: Eran_Z

Searching
'''

from RSM.my_movies.models import Movie, SearchResult

def searchSingle(term):
    """Returns number of hits for given term."""
    return getattr(Movie.objects.get(title=term),'searchSingle')

def searchTogether(term1, term2):
    """Returns number of hits for 2 given terms together."""
    if term1 == term2:
        return searchSingle(term1)
    m1 = Movie.objects.get(title=term1)
    m2 = Movie.objects.get(title=term2)
    return getattr(SearchResult.objects.get(movie1=m1,movie2=m2), 'numResults')




#------------- UNUSED EXTENSIONS --------------------

from math import log
from search.bingSearch import bingSearch


def __search(searchStr):
    """Searches given search string on the web.
    Returns number of hits."""
    return bingSearch.bingSearchResults(searchStr)

def searchExclusion(term, Ex):
    """Returns number of hits for given term,
    excluding pages containing terms from the given exclusion group."""
    #Note: this shouldn't be used with the current search options
    searchStr = "\"" + term + "\""
    for str in Ex:
        searchStr += " -\"" + str + "\""
    return __search(searchStr)

def NGD(x,y):
    logx = log(searchSingle(x))
    logy = log(searchSingle(y))
    logxy = log(searchTogether(x,y))
    logN = log(bingSearch.N)
    return (max(logx, logy) - logxy) / (logN - min(logx, logy))
    
